<?php
/**
 * Fang_Service_Manager
 * 
 * 作者:  姚刚 (yaogang@the9sense.com)
 * 创建时间: 2011-03-15 18:03:41
 * 修改记录: 
 * 
 * $Id: Manager.php 18 2011-04-17 12:50:07Z coosuu $
 */

class Fang_Service_Manager {

    /**
     * add 
     * 
     * @param  mixed $handle 
     * @param  mixed $name 
     * @param  mixed $password 
     * @param  mixed $email 
     * @param  mixed $mobile 
     * @param  mixed $qq 
     * @return void
     */
    public function add($handle, $name, $password, $email = null, $mobile = null, $qq = null) {

        $data = array('m_handle' => $handle,
                      'm_name' => $name,
                      'm_password' => $password,
                      'm_email' => $email,
                      'm_mobile' => $mobile,
                      'm_qq' => $qq);
        $data = array_filter($data);
        if (!$data) {
            return false;
        }

        try {
            return Fang_DAO_Manager::add($data);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * update 
     * 
     * @param  mixed $mid 
     * @param  mixed $handle 
     * @param  mixed $name 
     * @param  mixed $password 
     * @param  mixed $email 
     * @param  mixed $mobile 
     * @param  mixed $qq 
     * @return void
     */
    public function update($mid, $handle = null, $name = null, $password = null, $email = null, $mobile = null, $qq = null) {

        $data = array('m_handle' => $handle,
                      'm_name' => $name,
                      'm_password' => $password,
                      'm_email' => $email,
                      'm_mobile' => $mobile,
                      'm_qq' => $qq);
        $data = array_filter($data);
        if (!$data) {
            return false;
        }

        try {
            return Fang_DAO_Manager::update($mid, $data);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * listAll 
     * 
     * @param  array $pageOptions 
     * @param  array $fields 
     * @return void
     */
    public function listAll(&$pageOptions = array(), $fields = array()) {

        if ($pageOptions && !$pageOptions['totalItems']) {
            $pageOptions['totalItems'] = Fang_DAO_Manager::count();
        }
        $pageOptions = Pager::convert($pageOptions);

        try {
            return Fang_DAO_Manager::listAll($pageOptions, $fields);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * get 
     * 
     * @param  mixed $mid 
     * @param  array $fields 
     * @return void
     */
    public function get($mid, $fields = array()) {

        try {
            return Fang_DAO_Manager::get($mid, $fields);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * isExists 
     * 
     * @param  mixed $handle 
     * @return void
     */
    public function isExists($handle) {

        try {
            $manager = Fang_DAO_Manager::getByHandle($handle);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }

        if ($manager) {
            return true;
        }

        return false;
    }

    /**
     * getByHandle
     * 根据用户名取数据 
     * 
     * @param  mixed $handle 
     * @param  array $fields 
     * @return void
     */
    public function getByHandle($handle, $fields = array()) {

        try {
            return Fang_DAO_Manager::getByHandle($handle, $fields);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }
}
